package com.zzyl.serve.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.serve.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingProjectMapper;
import com.zzyl.serve.domain.NursingProject;
import com.zzyl.serve.service.INursingProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;

/**
 * 护理项目Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-30
 */
@Service
public class NursingProjectServiceImpl extends ServiceImpl<NursingProjectMapper, NursingProject> implements INursingProjectService
{
    @Autowired
    private NursingProjectMapper nursingProjectMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    private static final String NURSINGPROJECT_CACHE="nursingproject:all";

    /**
     * 查询护理项目
     *
     * @param id 护理项目主键
     * @return 护理项目
     */
    @Override
    public NursingProject selectNursingServiceById(Long id)
    {
        return this.getById(id);
    }

    /**
     * 查询护理项目列表
     *
     * @param nursingService 护理项目
     * @return 护理项目
     */
    @Override
    public List<NursingProject> selectNursingServiceList(NursingProject nursingService)
    {
        return nursingProjectMapper.selectNursingServiceList(nursingService);
    }

    /**
     * 新增护理项目
     *
     * @param nursingService 护理项目
     * @return 结果
     */
    @Override
    public int insertNursingService(NursingProject nursingService)
    {
        boolean save = this.save(nursingService);
//        nursingService.setCreateTime(DateUtils.getNowDate());
        deleteCache();
        return save?1:0;
    }

    /**
     * 修改护理项目
     *
     * @param nursingService 护理项目
     * @return 结果
     */
    @Override
    public int updateNursingService(NursingProject nursingService)
    {
        boolean b = this.updateById(nursingService);
//        nursingService.setUpdateTime(DateUtils.getNowDate());
        deleteCache();
        return b?1:0;
    }

    /**
     * 批量删除护理项目
     *
     * @param ids 需要删除的护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingServiceByIds(Long[] ids)
    {
        boolean b = this.removeByIds(Arrays.asList(ids));
        deleteCache();
        return b?1:0;
    }

    /**
     * 删除护理项目信息
     *
     * @param id 护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingServiceById(Long id)
    {
        boolean b = this.removeById(id);
        this.deleteCache();
        return b?1:0;
    }

    /**
     * 获取所有启用项目
     *
     * @return
     */
    @Override
    public List<NursingProjectVo> getAllProjects() {
        List<NursingProjectVo> list = (List<NursingProjectVo>) redisTemplate.opsForValue().get(NURSINGPROJECT_CACHE);
        if (CollectionUtils.isNotEmpty(list)){
            return list;
        }

       list= nursingProjectMapper.getAllProjects();
        redisTemplate.opsForValue().set(NURSINGPROJECT_CACHE,list);
        return list;
    }
    private void deleteCache(){
        redisTemplate.delete(NURSINGPROJECT_CACHE);
    }
}
